﻿using System;

namespace Iot.Shared.Path;

/// <summary>
///     消息总线数据路径地址
/// </summary>
public sealed record DevicePath : IFormattable /*, IEquatable<DevicePath>*/
{
    private string? _path;

    /// <summary>
    /// </summary>
    /// <param name="name">数据名称</param>
    /// <param name="super">父路径地址</param>
    internal DevicePath(string name, DevicePath? super)
    {
        Name = name;
        Super = super;
    }


    /// <summary>
    ///     数据名称
    /// </summary>
    public string Name { get; }

    /// <summary>
    ///     父路径地址
    /// </summary>
    public DevicePath? Super { get; }


    public bool Equals(DevicePath? other)
    {
        return other != null && Name == other.Name && Super == other.Super;
    }


    public string ToString(string? format, IFormatProvider? formatProvider)
    {
        format ??= SeparatorStr;
        return Super is null ? Name : $"{Super.ToString(format, formatProvider)}{format}{Name}";
    }


    public override string ToString()
    {
        return _path ??= ToString(null, null);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Name, Super);
    }


    #region 路径分隔符

    /// <summary>
    ///     路径分隔符
    /// </summary>
    public const char Separator = '.';

    /// <summary>
    ///     路径分隔符
    /// </summary>
    public const string SeparatorStr = ".";

    #endregion
}